package com.codegenerate.demo.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codegenerate.demo.config.R;
import com.codegenerate.demo.dto.GFieldDto;
import com.codegenerate.demo.entity.GField;
import com.codegenerate.demo.service.impl.GFieldServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@RestController
@RequestMapping("/gTable/gField")
public class GFieldController {
    @Autowired
    GFieldServiceImpl gFieldService;


    @PostMapping("/list")
    R<List<GField>> list(@RequestBody GFieldDto gFieldDto){
        Page<GField> page = gFieldService.page(
                gFieldDto.buildPage(),gFieldDto.dto2entity().getQueryAll()
        );
        return R.ok(page.getRecords())
                .setCount(page.getTotal());
    }

    @GetMapping("/getItem")
    R<GField> getItem(int id) {
        return R.ok(gFieldService.getById(id));
    }

    @PostMapping("/add")
    R<Boolean> add(@RequestBody  GFieldDto gFieldDto){
        var entity=gFieldDto.dto2entity();
        //设置 默认
        GField.setDefaultValue(entity);
        return R.ok(gFieldService.save(entity));
    }


    @PostMapping("/update")
    R<Integer> update(@RequestBody  GFieldDto gFieldDto){
        gFieldDto.validateUpdate();
        return R.ok(gFieldService.getBaseMapper().update(null, gFieldDto.dto2entity().getUpdateWrapper()));
    }

    @GetMapping("/del")
    R<Boolean> del(@RequestParam("id") int id) {
        return R.ok(gFieldService.removeById(id));
    }
}

